package cn.agiledata.bank.interest.service;

import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

import cn.agiledata.bank.common.util.DateUtil;

/***
 * 计息通用部分业务逻辑
 * @author jessik
 * */
public class InterestCore { 
	final static Logger log = Logger.getLogger(InterestCore.class);
	/**
	 * 两个时间所间隔的实际天数 <br/>
	 * 
	 * @param begin 起始日期
	 * @param end  截至日期
	 */
	public final static int getNaturalDays(Date begin, Date end){
		int days = DateUtil.naturalDaysBetween(begin,end);
		log.info("getNaturalDays()"+days+"d = end("+end+") - begin("+begin+");");
		return days; 
	}
	
	private final static int getNaturalDays_(Date begin, Date end) {
		
		long distance = end.getTime() - begin.getTime();
		
		int days = (int) (distance / (1000 * 3600 * 24));
		
		log.info("getNaturalDays()"+days+"d("+distance
				+" ms) = end("+end+") - begin("+begin+");");
		return days;
	}
	/**
	 * 两个时间所间隔的银行利息天数
	 * @param begin 起始日期
	 * @param end  截至日期
	 */
	public final static int getBankDays(Date begin, Date end) {
		Calendar calendar = Calendar.getInstance();
		
		calendar.setTime(end);
		int yearEnd = calendar.get(Calendar.YEAR);
		int monthEnd = calendar.get(Calendar.MONTH);
		int dayEnd = calendar.get(Calendar.DATE);
		calendar.setTime(begin);
		int yearBegin = calendar.get(Calendar.YEAR);
		int monthBegin = calendar.get(Calendar.MONTH);
		int dayBegin = calendar.get(Calendar.DATE);
		
		int days = (yearEnd - yearBegin)*360+(monthEnd - monthBegin)*30+(dayEnd - dayBegin);
		String temp = ("getBankDays()"+days+"d=(yearEnd("+yearEnd
				+") - yearBegin("+yearBegin
				+"))*360+(monthEnd("+monthEnd
				+") - monthBegin("+monthBegin
				+"))*30+(dayEnd("+dayEnd+") - begin("+dayBegin+"));"+"("+begin+"-"+end+")");
		/**特殊之处*/
		if(dayBegin==31){
			days-=1;
			temp+="dayBegin==31(-1);";
		}
		if(dayEnd==31){
			days-=1;
			temp+="dayEnd==31(-1);";
		}
		log.info(temp);
		return days;
	}

	/**
	 * 获取最新 某个利率，IRNum：利率号
	 * @deprecated
	 */
	public float getInterestRate(int param7) {
		return 0;
	}

	
	public static final int RATE_ANNUAL = 1;

	public static final int RATE_MONTHLY = 2;

	public static final int RATE_DAILY = 3;
	/**
	 * 年、月、日利率转换
	 * 
	 * @param inType传入值的类型
	 * @param outType输出值的类型
	 * @return 输出值，保留任意多位小数。注意：资金额足够大时使用该返回值计算的利息可能会有误差
	 */
	public final static float getRate(float rateValue, int inType, int outType) throws Exception{
		if(inType==RATE_ANNUAL&&outType==RATE_DAILY){
			log.debug("getRate()"+(rateValue/(100*360)));
			return rateValue/(100*360);
		}
		throw new Exception("-----------not known inType/outType value");
	}
/**
举例：存入日  2006.10.30   2006.10.30   2006.10.31
      支取日  2006.10.31   2006.11.2    2006.11.2
      天  数       0          2          2
2、2月问题。
举例：
存入日  2006.2.28  2004.2.29  2006.2.28  2006.2.28   2006.2.28   2006.2.28
   支取日  2006.3.1   2004.3.1   2006.3.28  2006.3.29   2006.3.30   2006.3.31
   天  数    3          2         30         31        32         32

 * */
	public static void main(String[] args) {
		InterestCore interestCore = new InterestCore();
		Date begin = new Date("2006/12/6 14:39:06");
		Date end = new Date(Date.parse("2007/1/17 16:52:50"));
//		Date end=new Date();
//		interestCore.getBankDays(null, null);

		System.err.println(interestCore.getNaturalDays(begin, end));

	}
}
